/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-10-06 21:08:50
 * Last Modified: 2017-07-11 10:58:07
 * File Name: controllers/account.go
 * Description:
 */
package controllers

import (
	"encoding/json"

	"qkleague/account"
	"qkleague/errorcode"

	log "github.com/cihub/seelog"
)

type AccountController struct {
	//	URLSignController
	SSOLoginedController
}

/*
	用户在加盟商系统注册
*/
// @router /register [post]
func (p *AccountController) Register() {
	defer p.Output("Register")

	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body: %s", string(body))
	req := &account.QKLeague{}
	if err := json.Unmarshal(body, &req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	if req.AccountId == "" || len(req.AccountId) < 6 {
		p.Resp["code"] = errorcode.EC_ACCOUNT_MOBILE_INVALID
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_ACCOUNT_MOBILE_INVALID)
		return
	}
	code := account.GetAccountService().Register(req)
	if code == errorcode.EC_GL_SUCCESS {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
		return
	}
	p.Resp["code"] = code
	p.Resp["msg"] = errorcode.Msg(code)
}

/*
	用户修改信息
*/
// @router /modify [post]
func (p *AccountController) Modify() {
	defer p.Output("Modify")

	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body: %s", string(body))
	data := make(map[string]interface{})
	if err := json.Unmarshal(body, &data); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	if _, ok := data["userid"]; !ok {
		p.Resp["code"] = errorcode.EC_USER_INFO_MODIFY_USER_ID_MISSING
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_USER_INFO_MODIFY_USER_ID_MISSING)
		return
	}

	code := account.GetAccountService().UpdateUserInfo(data)
	p.Resp["code"] = code
	p.Resp["msg"] = errorcode.Msg(code)

}
